package com.yitao.cms.service.shopInfo;

import com.skipper.base.bean.DataOutput;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.shopInfo.ShopConfigDto;
import com.yitao.client.model.shopInfo.QShopConfigModel;
import com.yitao.client.model.shopInfo.ShopConfigModel;
import com.yitao.cms.constant.UserErrorEnum;
import com.yitao.cms.constant.UserSession;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import com.yitao.cms.utils.BaseUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: wwl
 * @Date: 2019/8/22 15:29
 * @Description:
 */
@Service
public class ShopConfigService extends GenericEntityService<ShopConfigModel, ShopConfigDto, QShopConfigModel> {
    public ShopConfigService(EntityManager entityManager) {
        super(entityManager);
    }

    BaseUtils baseUtils = BaseUtils.getInstance();

    @Transactional
    public DataOutput addShopConfig(HttpServletRequest request, String serverCodeUrl) {
        //判断是否登录
        OperationUserCmsModel operationUserCmsModel = UserSession.getUserSession(request);
        if (operationUserCmsModel == null || operationUserCmsModel.getId() == null) {
            return new DataOutput(UserErrorEnum.NULL_LOGIN.getCode(), UserErrorEnum.NULL_LOGIN.getDesc());
        }
        //参数判断
       /* if (StringUtils.isBlank(serverCodeUrl)) {
            return baseUtils.getBadErrorMsg("客服二维码地址不能为空!");
        }*/
        if (operationUserCmsModel.getShopId() == null) {
            return baseUtils.getBadErrorMsg("门店Id不能为空!");
        } else {
            //判断是否已绑定客服二维码，有则删除
            List<ShopConfigModel> list = repository.findAll(Q.shopId.eq(operationUserCmsModel.getShopId()).and(Q.isDelete.eq(false)));
            if (!CollectionUtils.isEmpty(list)) {
                repository.updateByWhere(Q.shopId.eq(operationUserCmsModel.getShopId()).and(Q.isDelete.eq(false)),
                        it -> it.set(Q.isDelete, true)
                                .set(Q.lastUpdateBy, operationUserCmsModel.getId())
                                .set(Q.lastUpdateTime, baseUtils.getNow()));
            }
            if(StringUtils.isNotBlank(serverCodeUrl)) {
                //添加数据
                ShopConfigModel shopConfigModel = new ShopConfigModel();
                shopConfigModel.setShopId(operationUserCmsModel.getShopId());
                shopConfigModel.setServerCodeUrl(serverCodeUrl);
                shopConfigModel.setDelete(false);
                shopConfigModel.setCreateBy(operationUserCmsModel.getId());
                shopConfigModel.setCreateTime(baseUtils.getNow());
                ShopConfigModel shopConfigModel1 = repository.save(shopConfigModel);
                return new DataOutput(convertEntityToDto(shopConfigModel1));
            }
            return baseUtils.getSuccessMsg();

        }
    }

    /**
     * 查询门店客服二维码
     * @param
     * @return
     */
     public ShopConfigModel findShopConfigByShopId(Long shopId){
         if (shopId==null){
             return null;
         }else{
           List<ShopConfigModel> shopConfigModels =  repository.findAll(Q.shopId.eq(shopId).and(Q.isDelete.eq(false)));
             //ShopConfigModel shopConfigModel = queryFactory.select(Q).where(Q.shopId.eq(shopId).and(Q.isDelete.eq(false))).fetchFirst();
             if (CollectionUtils.isEmpty(shopConfigModels)){
                 return null;
             }else{
                 return shopConfigModels.get(0);
             }
         }
     }
}
